package com.javaweb.web.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.javaweb.annotation.token.TokenDataAnnotation;
import com.javaweb.annotation.url.ControllerMethod;
import com.javaweb.base.BaseResponseResult;
import com.javaweb.base.BaseService;
import com.javaweb.base.BaseSystemMemory;
import com.javaweb.base.po.Dictionary;
import com.javaweb.base.po.Interfaces;
import com.javaweb.base.po.User;
import com.javaweb.constant.ApiCommonConstant.LoginAccessConstant;
import com.javaweb.constant.CommonConstant;
import com.javaweb.constant.SystemConstant;
import com.javaweb.enums.AuthEnum;
import com.javaweb.enums.HttpCodeEnum;
import com.javaweb.web.eo.TokenData;
import com.javaweb.web.eo.TokenDataEasyResponse;
import com.javaweb.web.eo.interfaces.InterfaceInfoResponse;

/**
 * 登录才可访问的模块
 * <p>
 * 该模块提供了用户登出接口、获取redis中的token信息接口、获得字典信息接口
 * 
 * @author tank2140896
 */
@RestController
@RequestMapping(LoginAccessConstant.WEB_LOGIN_ACCESS_PREFIX)
public class LoginAccessController extends BaseService {
	
	/**
	 * 用户登出接口
	 * @param tokenData tokenData
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@GetMapping(LoginAccessConstant.LOGIN_OUT)
	@ControllerMethod(interfaceName="用户登出接口",auth=AuthEnum.LOGIN)
	public BaseResponseResult logout(@TokenDataAnnotation TokenData tokenData) {
		if(deleteFromRedisByKey(getRedisTokenKey(tokenData))) {
			return getBaseResponseResult(HttpCodeEnum.SUCCESS,"login.user.logoutSuccess");
		}else {
			return getBaseResponseResult(HttpCodeEnum.SUCCESS,"login.user.logoutFail");
		}
	}
	
	/**
	 * 获取redis中的token信息接口
	 * @param tokenData tokenData
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@GetMapping(LoginAccessConstant.GET_REDIS_TOKEN_DATA)
	@ControllerMethod(interfaceName="获取redis中的token信息接口",auth=AuthEnum.LOGIN)
	public BaseResponseResult getRedisTokenData(@TokenDataAnnotation TokenData tokenData) {
		User user = tokenData.getUser();
		TokenDataEasyResponse tokenDataEasyResponse = new TokenDataEasyResponse();
		tokenDataEasyResponse.setUserName(user.getUserName());
		tokenDataEasyResponse.setRsaPublicKey1(tokenData.getRsaPublicKey1());
		tokenDataEasyResponse.setRsaPrivateKey2(tokenData.getRsaPrivateKey2());
		tokenDataEasyResponse.setMenuListForTree(tokenData.getMenuListForTree());
		/** 这里不需要太多的[华丽操作]，采用[朴实无华]的双层for循环即可 start */
		List<String> apiUrlList = tokenData.getApiUrlList();
		List<InterfaceInfoResponse> interfaceInfoResponseList = new ArrayList<>();
		if(!CollectionUtils.isEmpty(apiUrlList)) {
			List<Interfaces> interfacesList = BaseSystemMemory.interfacesList;
			for(int i=0;i<apiUrlList.size();i++){
				for(int j=0;j<interfacesList.size();j++){
					Interfaces interfaces = interfacesList.get(j);
					if(apiUrlList.get(i).equals(interfaces.getUrl())){
						interfaceInfoResponseList.add(new InterfaceInfoResponse(interfaces.getUrl(),interfaces.getRequestDataSecret(),interfaces.getResponseDataSecret()));
						break;
					}
				}
			}
		}
		tokenDataEasyResponse.setInterfaceInfoResponseList(interfaceInfoResponseList);
		/** 这里不需要太多的[华丽操作]，采用[朴实无华]的双层for循环即可 end */
		tokenDataEasyResponse.setExcludeInfoResponseList(tokenData.getExcludeInfoResponseList());
		tokenDataEasyResponse.setPageUrlList(tokenData.getPageUrlList());
		tokenDataEasyResponse.setAliasList(tokenData.getAliasList());
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"login.user.getTokenDataSuccess",tokenDataEasyResponse);
	}
	
	/**
	 * 获得字典信息接口
	 * @param dictionary 字典请求参数
	 * @return BaseResponseResult 接口通用返回参数
	 */
    @PostMapping(LoginAccessConstant.GET_DICTIONARY)
    @ControllerMethod(interfaceName="获得字典信息接口",auth=AuthEnum.LOGIN)
    public BaseResponseResult getDictionary(@RequestBody Dictionary dictionary) {
	    return getBaseResponseResult(HttpCodeEnum.SUCCESS,"login.user.getDictionarySuccess",dictionaryService.getDictionary(dictionary));
	}
    
	/**
	 * 获得平台信息接口
	 * <p>
	 * 目前未做分页未做查询，简单处理，直接获取所有平台信息
	 * @return BaseResponseResult 接口通用返回参数
	 */
    @GetMapping(LoginAccessConstant.GET_PLATFORM)
    @ControllerMethod(interfaceName="获得平台信息接口",auth=AuthEnum.LOGIN)
    public BaseResponseResult getPlatform() {
    	Set<String> set = stringRedisTemplate.keys(SystemConstant.APP_KEY_PREFIX+"*");
    	List<String> list = Collections.emptyList();
    	if(!CollectionUtils.isEmpty(set)) {
    		list = set.stream().map(e->e.split(CommonConstant.UNDERLINE)[1]).toList();
    	}
	    return getBaseResponseResult(HttpCodeEnum.SUCCESS,"login.user.getPlatformSuccess",list);
	}
    
}
